<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * category_model
 *  有奖游戏管理模型
 */
class Prize_model extends CI_Model
{
    var $_table_name = 'game_prize';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $page 分页开始数
     * @param $offset  分页数量
     * @param array $view 搜索关键词
     * @return array
     */
    public function get_list($page, $offset, $view)
    {
        $this->db->select('h.*,c.title as client_title');
        $this->db->from("$this->_table_name h");
        $this->db->join('client as c', 'c.id=h.client_id', 'left outer');
        if (!empty($view['title'])) {
            $this->db->like('h.title', $view['title']);
        }
        if (!empty($view['category_id'])) {
            $this->db->where('h.category_id', $view['category_id']);
        }
        if (!empty($view['client_id'])) {
            $this->db->where('h.client_id', $view['client_id']);
        }
        if (!empty($view['cityid'])) {
            $this->db->where('h.cityid', $view['cityid']);
        }
        if (empty($view['admin'])) {
            $this->db->where('h.status', 0);
            $this->db->where('h.endtime >', time());
        }
        $this->db->order_by("h.sort desc,h.click desc,h.createtime desc");
        $this->db->limit($offset, $page);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * 获取总量
     * @param array $view 搜索关键词
     * @return array
     */
    public function total_rows($view)
    {
        $this->db->select('count(*) as num');
        $this->db->from("$this->_table_name");

        if (isset($view['title'])) {
            $this->db->like('title', $view['title']);
        }
        if (isset($view['category_id'])) {
            $this->db->where('category_id', $view['category_id']);
        }
        if (isset($view['client_id'])) {
            $this->db->where('client_id', $view['client_id']);
        }
        if (empty($view['admin'])) {
            $this->db->where('status', 0);
            $this->db->where('endtime >', time());
        }
        $query = $this->db->get();
        $row = $query->row_array();

        return $row['num'];
    }

    /**
     * 获取相关有奖游戏
     */
    public function category_list($id, $category_id)
    {
        $this->db->select('*');
        $this->db->from("$this->_table_name");
        $this->db->where('category_id', $category_id);
        $this->db->where('id !=', $id);
        $this->db->order_by("sort", "desc");
        $this->db->limit(0, 4);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     *  添加一个有奖游戏城市数目+1
     */
    public function update($id)
    {
        $sql = "UPDATE 17koko_area 
                SET prize_sum=prize_sum+1 
                WHERE here id={$id}";

        return $this->db->query($sql);
    }

    /**
     *  根据ID获取一个信息
     */
    public function get_one($id)
    {
        $this->db->select('h.*,c.title as client_title');
        $this->db->from("$this->_table_name h");
        $this->db->join('client as c', 'c.id=h.client_id', 'left outer');
        $this->db->where('h.status', 0);
        $this->db->where('h.endtime >', time());
        $this->db->where('h.id', $id);
        $query = $this->db->get();

        return $query->row_array();
    }
}